package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageBean;
import com.zzyl.dto.OrderQueryDto;
import com.zzyl.entity.Order;
import com.zzyl.mapper.OrderMapper;
import com.zzyl.service.OrderService;
import com.zzyl.vo.OrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    /**
     * 根据订单id获取订单详情
     *
     * @param orderId 订单id
     * @return 订单vo
     */
    @Override
    public OrderVo getOrderById(Long orderId) {
        Order order = orderMapper.selectByPrimaryKey(orderId);
        return BeanUtil.toBean(order, OrderVo.class);
    }


    @Override
    public PageBean<OrderVo> searchOrders(OrderQueryDto dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());

        LocalDateTime startTime = null;
        LocalDateTime endTime = null;

        if (ObjectUtil.isNotEmpty(dto.getStartTime())) {
            startTime = LocalDateTimeUtil.of(dto.getStartTime());
        }

        if (ObjectUtil.isNotEmpty(dto.getEndTime())) {
            endTime = LocalDateTimeUtil.of(dto.getEndTime());
        }

        Page<Order> lists = orderMapper.searchOrders(dto.getStatus(),
                dto.getOrderNo(),
                dto.getElderlyName(),
                dto.getCreator(),
                startTime,
                endTime,
                dto.getUserId());

        return PageBean.of(lists, OrderVo.class);
    }

    @Override
    public List<OrderVo> listByMemberId(Long id) {
        return orderMapper.listByMemberId(id);
    }

    @Override
    public List<Order> selectByStatus(Integer status) {
        return orderMapper.selectByStatus(status);
    }
}

